package cn.edu.jxau.test;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author Fudashi
 */
public class Main {

	public static void main(String[] args) {

//		int[][] m = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
		// System.out.println(search(m, 7));
		// System.out.println(search(m, 9));
		// System.out.println(search(m, 6));
		// System.out.println(search(m, 1));
		// System.out.println(search(m, 15));
		// System.out.println(search(m, 0));
		// System.out.println(search(m, 16));
		// System.out.println(search(null,15));
		int[][] m = new int[2][];
		m[0] = new int[2];
		m[1] = new int[5];
		System.out.println(search(m, 23));
	}

	public static boolean search(int[][] m, int target) {

		// 参数校验，特殊情况处理 //
		Objects.requireNonNull(m, "参数m不能为null");
		if (m.length == 0 || m[0].length == 0) { // m为空矩阵
			return false;
		}
		int len = m[0].length;
		for (int i = 1; i < m.length; i++) {
			if (m[i].length != len) {
				throw new IllegalArgumentException("参数m不是矩阵");
			}
		}

		// 定位右上角//
		int i = 0;
		int j = m[0].length - 1;
		while (i < len && j >= 0) { // 定位元素
			if (m[i][j] > target) {
				j--;
			} else if (m[i][j] < target) {
				i++;
			} else {
				return true;
			}
		}
		return false;
	}
}
